import utils from '../core/utils';
import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import { clientBase } from './core/clientBase';
import regionModel from '../model/other/regionModel';
import returnModel from '../model/common/returnModel';
import rankItemModel from '../model/other/rankItemModel';
import regionTagItem from '../model/videos/regionTagItem';
import regionVideoItem from '../model/videos/regionVideoItem';

class channelClient extends clientBase {
  constructor() {
    super();
  }

  getRank(rid: number, type: string): Promise<returnModel<rankItemModel[]>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.bilibili.com/x/web-interface/ranking/v2", `&rid=${rid}&type=${type.toLowerCase()}`);
        this.client.getResult(info).then((res) => {
          let arr: Array<rankItemModel> = res.data.data.list;
          resolve(this.buildSuccessResult<rankItemModel[]>(arr));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<rankItemModel[]>(e));
      }
    });
  }

  setRegions(): Promise<returnModel<regionModel>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://app.bilibili.com/x/v2/region/index");
        this.client.getResult(info).then((res) => {
          let data: regionModel = res;
          resolve(this.buildSuccessResult<regionModel>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<regionModel>(e));
      }
    });
  }

  regionDynamic(rid: number, next?: string): Promise<returnModel<Array<regionVideoItem>>> {
    return new Promise((resolve) => {
      try {
        let info;
        if (utils.isEmpty(next))
          info = new apiInfo(`https://app.bilibili.com/x/v2/region/dynamic`, `&rid=${rid}`);
        else
          info = new apiInfo(`https://app.biliapi.net/x/v2/region/dynamic/list`, `&rid=${rid}&ctime=${next}&pull=false`);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<regionVideoItem> = res.data.new;
            if (utils.isEmpty(next)) {
              let arr: Array<regionTagItem> = res.data?.recommend;
              arr?.push(new regionTagItem(0, '全部标签'));
            }
            let nextId = res.data.cbottom.toString();
            resolve(this.buildSuccessResult<Array<regionVideoItem>>(data, nextId));
          } else
            resolve(this.buildFailedResult<Array<regionVideoItem>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<regionVideoItem>>(e));
      }
    });
  }

  regionChildDynamic(rid: number, tagId: number = 0, next: string = ''): Promise<returnModel<Array<regionVideoItem>>> {
    return new Promise((resolve) => {
      try {
        let info;
        if (utils.isEmpty(next))
          info = new apiInfo(`https://app.biliapi.net/x/v2/region/dynamic/child`, `&rid=${rid}&tag_id=${tagId}&pull=true`);
        else
          info = new apiInfo(`https://app.bilibili.com/x/v2/region/dynamic/child/list`, `&rid=${rid}&tag_id=${tagId}&pull=false&ctime=${next}`);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<regionVideoItem> = res.data.new;
            if (utils.isEmpty(next)) {
              let arr: Array<regionTagItem> = res.data?.top_tag;
              arr?.push(new regionTagItem(0, '全部标签'));
            }
            let nextId = res.data.cbottom;
            resolve(this.buildSuccessResult<Array<regionVideoItem>>(data, nextId));
          } else
            resolve(this.buildFailedResult<Array<regionVideoItem>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<regionVideoItem>>(e));
      }
    });
  }

  regionChildList(rid: number, order: string, page: number, tagId: number): Promise<returnModel<Array<regionVideoItem>>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo(`https://app.biliapi.net/x/v2/region/show/child/list`
          , `&order=${order}&pn=${page}&ps=20&rid=${rid}&tag_id=${tagId}`);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<regionVideoItem> = res.data;
            resolve(this.buildSuccessResult<Array<regionVideoItem>>(data));
          } else
            resolve(this.buildFailedResult<Array<regionVideoItem>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<regionVideoItem>>(e));
      }
    });
  }
}
export default new channelClient();